package com.TeacherInfoServer.common.service.Impl;

import com.TeacherInfoServer.exception.RecordExistDetailException;
import com.TeacherInfoServer.listener.ExcelRespProp;
import com.TeacherInfoServer.mapper.TeacherInfoChangeMapper;
import com.TeacherInfoServer.pojo.DTO.SearchDTO.TeacherInfoChangeSearchDTO;
import com.TeacherInfoServer.pojo.DTO.TeacherInfoChangeDTO;
import com.TeacherInfoServer.pojo.DTO.UpdateTeacherInfoChangeDTO;
import com.TeacherInfoServer.pojo.entity.TeacherInfoChange;
import com.TeacherInfoServer.pojo.vo.TeacherInfoChangeListVo;
import com.TeacherInfoServer.common.service.TeacherInfoChangeService;
import com.TeacherInfoServer.common.service.TypeInfoService;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TeacherInfoChangeServiceImpl extends ServiceImpl<TeacherInfoChangeMapper, TeacherInfoChange> implements TeacherInfoChangeService {
    @Autowired
    public TeacherInfoChangeMapper teacherInfoChangeMapper;
    @Autowired
    public TypeInfoService typeInfoService;

    @Override
    public Map<String, Object> getList(Integer pages, Integer size){
        Page<TeacherInfoChange> page = new Page<>(pages, size);
        QueryWrapper<TeacherInfoChange> queryWrapper = new QueryWrapper<>();
        teacherInfoChangeMapper.getTeacherInfoChangeByPage(page,queryWrapper);
        Map<String, Object> data = new HashMap<>();
        data.put("totals",page.getTotal());
        data.put("currentPages",page.getCurrent());
        data.put("pageNumber",page.getPages());
        data.put("rows",page.getRecords());
        return data;
    }
    @Override
    public Map<String,Object> searchTeacherInfoChangeList(TeacherInfoChangeSearchDTO teacherInfoChangeSearchDTO) {
        Page<?> page = new Page<>(teacherInfoChangeSearchDTO.getPages(), teacherInfoChangeSearchDTO.getSize());
        teacherInfoChangeMapper.searchTeacherInfoChangeList(page, teacherInfoChangeSearchDTO);
        Map<String, Object> data = new HashMap<>();
        data.put("totals", page.getTotal());
        data.put("currentPages", page.getCurrent());
        data.put("pageNumber", page.getPages());
        data.put("rows", page.getRecords());
        return data;
    }

    @Transactional
    @Override
    public void addTeacherInfoChange(TeacherInfoChangeDTO teacherInfoChangeDTO){
        TeacherInfoChange teacherInfoChange=new TeacherInfoChange();
        QueryWrapper<TeacherInfoChange> queryWrapper = new QueryWrapper<>();
        if(teacherInfoChangeMapper.selectOne(queryWrapper.eq("teacher_id",teacherInfoChangeDTO.getTeacherId())
                .eq("change_type",typeInfoService.getOneId("变动类型",teacherInfoChangeDTO.getChangeType()))
                .eq("change_date",teacherInfoChangeDTO.getChangeDate())
                .eq("change_reason",teacherInfoChangeDTO.getChangeReason())) == null) {
            teacherInfoChange.setTeacherId(teacherInfoChangeDTO.getTeacherId());
            teacherInfoChange.setChangeType(typeInfoService.getOneId("变动类型",teacherInfoChangeDTO.getChangeType()));
            teacherInfoChange.setChangeDate(teacherInfoChangeDTO.getChangeDate());
            teacherInfoChange.setChangeReason(teacherInfoChangeDTO.getChangeReason());
            teacherInfoChangeMapper.insert(teacherInfoChange);
        }
        else {
            throw new RecordExistDetailException("已有完全相同记录");
        }
    }

    @Transactional
    @Override
    public void updateTeacherInfoChange(UpdateTeacherInfoChangeDTO teacherInfoChange){
        QueryWrapper<TeacherInfoChange> queryWrapper = new QueryWrapper<>();
        if(!teacherInfoChangeMapper.selectList(queryWrapper
                .eq("id",teacherInfoChange.getId())
                .eq("teacher_id",teacherInfoChange.getTeacherId())
                .eq("change_type",typeInfoService.getOneId("变动类型",teacherInfoChange.getChangeType()))
                .eq("change_date",teacherInfoChange.getChangeDate())
                .eq("change_reason",teacherInfoChange.getChangeReason())).isEmpty()) {

            throw new RecordExistDetailException("未进行更新");
        }
        QueryWrapper<TeacherInfoChange> queryWrapper1 = new QueryWrapper<>();
        if(!teacherInfoChangeMapper.selectList(queryWrapper1
                .eq("teacher_id",teacherInfoChange.getTeacherId())
                .eq("change_type",typeInfoService.getOneId("变动类型",teacherInfoChange.getChangeType()))
                .eq("change_date",teacherInfoChange.getChangeDate())
                .eq("change_reason",teacherInfoChange.getChangeReason())).isEmpty()) {

            throw new RecordExistDetailException("已有完全相同的记录");
        }

        UpdateWrapper<TeacherInfoChange> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", teacherInfoChange.getId())
                .set("teacher_id", teacherInfoChange.getTeacherId())
                .set("change_type", typeInfoService.getOneId("变动类型",teacherInfoChange.getChangeType()))
                .set("change_date", teacherInfoChange.getChangeDate())
                .set("change_reason", teacherInfoChange.getChangeReason());
        teacherInfoChangeMapper.update(null,updateWrapper);


    }

    @Transactional
    @Override
    public boolean deleteTeacherInfoChange(List<Long> ids){
        if (ids == null || ids.isEmpty()) {
            return false;
        }

        QueryWrapper<TeacherInfoChange> queryWrapper= new QueryWrapper<>();
        queryWrapper.in("id",ids);
        int deleteCount=teacherInfoChangeMapper.delete(queryWrapper);
        return deleteCount>0;
    }
    @Override
    @SneakyThrows(IOException.class)
    public void exportInfo(HttpServletResponse response){
        ExcelRespProp.setExcelRespProp(response, "教师信息变动列表");
        List<TeacherInfoChangeListVo> teacherInfoChanges = teacherInfoChangeMapper.getTeacherInfoChangeList();
        EasyExcel.write(response.getOutputStream())
                .head(TeacherInfoChangeListVo.class)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet("教师信息变动列表")
                .doWrite(teacherInfoChanges);

    }

}
